/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#include "targetnavigationwidget.h"
#include "ui_targetnavigationwidget.h"

#include "gps/gps.h"
#include "client/core/target.h"
#include "util/timerbox.h"

using namespace greenhills::client;
using namespace greenhills::client::core;
using namespace greenhills::gps;
using namespace greenhills::util;

CTargetNavigationWidget::CTargetNavigationWidget(CGPS *pGps, QWidget *parent)
        : CAutoLayoutWidget(parent), mUi(new Ui::CTargetNavigationWidget), mGps(pGps)
{
    mUi->setupUi(this);

    QObject::connect(mGps, SIGNAL(statusChanged()),
        this, SLOT(onStatusChanged()));

    QObject::connect(CTarget::instance(), SIGNAL(targetChanged()),
        this, SLOT(onTargetChanged()));

    CAutoLayoutWidget::addWidget(mUi->widget);
    CAutoLayoutWidget::addWidget(mUi->mCompas);

    //direct compass connection
    //QObject::connect(mGps, SIGNAL(movementDirectionChanged(int)),
    //mUi->mCompas, SLOT(onDirectionChanged(int)));

    //delayed compass connection for walking direction
    mTimerFace = new CTimerBox(this);
    QObject::connect(mGps, SIGNAL(movementDirectionChanged(int)),
      mTimerFace, SLOT(onValueChange(int)));
    QObject::connect(mTimerFace, SIGNAL(timerRepeater(int)),
       mUi->mCompas, SLOT(onDirectionChanged(int)));

     mTimerHand = new CTimerBox(this);
     //QObject::connect(mGps, SIGNAL(movementDirectionChanged(int)),
      //  mTimerFace, SLOT(onValueChange(int)));
     QObject::connect(mTimerHand, SIGNAL(timerRepeater(int)),
      mUi->mCompas, SLOT(onDirectionToTargetChanged(int)));
}

CTargetNavigationWidget::~CTargetNavigationWidget()
{
    delete mUi;
}

void CTargetNavigationWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        mUi->retranslateUi(this);
        break;
    default:
        break;
    }
}

void CTargetNavigationWidget::onStatusChanged()
{
    if (mGps->fix().isValid())
    {
        mUi->laUserLat->setText(mGps->position().latitude().toString());
        mUi->laUserLon->setText(mGps->position().longitude().toString());
        mUi->laUserHeading->setText(QString::number(mGps->movementDirection()));
        mUi->laUserSpeed->setText(QString::number(mGps->movementSpeed()));
        //mUi->mCompas->onDirectionChanged(mGps->movementDirection());
        //mTimerFace->onValueChange(mGps->movementDirection());
    }
    else
    {
        mUi->laUserLat->setText("-");
        mUi->laUserLon->setText("-");
        mUi->laUserHeading->setText("-");
        mUi->laUserSpeed->setText("-");
        //mUi->mCompas->onDirectionChanged(0);
        //mTimerFace->onValueChange(0);
    }
    onTargetDirectionOrDistanceChanged();
}

void CTargetNavigationWidget::onTargetChanged()
{
    if (CTarget::isSet())
    {
        mUi->laTargetLat->setText(CTarget::position().latitude().toString());
        mUi->laTargetLon->setText(CTarget::position().longitude().toString());
    }
    else
    {
        mUi->laTargetLat->setText("-");
        mUi->laTargetLon->setText("-");
    }
    onTargetDirectionOrDistanceChanged();
}

void CTargetNavigationWidget::onTargetDirectionOrDistanceChanged()
{
    int directionToTarget = 0;
    int distanceToTarget = 0;

    if (mGps->fix().isValid() && CTarget::isSet())
    {
        directionToTarget = mGps->position().directionTo(CTarget::position());
        distanceToTarget = mGps->position().distanceTo(CTarget::position());
        mUi->laTargetBearing->setText(QString::number(directionToTarget));
        mUi->laTargetDistance->setText(QString::number(distanceToTarget));
    }
    else
    {
        mUi->laTargetBearing->setText("-");
        mUi->laTargetDistance->setText("-");
    }    
       mTimerHand->onValueChange(directionToTarget);
        //mUi->mCompas->onDirectionToTargetChanged(directionToTarget);
}
